/*
 * @Author: 刘慧东
 * @Date: 2024-07-24 15:47:09
 * @LastEditors: 刘慧东
 * @LastEditTime: 2024-07-26 11:41:54
 */
package main

import (
	"context"
	"fmt"
	"net"
	"net/http"

	"github.com/ybbus/jsonrpc/v3"
)

type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

func main() {

	// socketPath := "/tmp/my_rpc.sock"

	transport := &http.Transport{
		DialContext: func(_ context.Context, network string, addr string) (net.Conn, error) {
			fmt.Printf("network: %s, addr: %s\n", network, addr)
			// return net.Dial("unix", socketPath)
			return net.Dial(network, addr)
		},
	}

	// 创建 HTTP 客户端
	client := &http.Client{
		Transport: transport,
	}

	// url := "http://localhost:2049/rpc"
	url := "http://192.168.146.101:52032/rcp/helloworld"

	rpcClient := jsonrpc.NewClientWithOpts(url, &jsonrpc.RPCClientOpts{
		HTTPClient: client,
	})

	ctx := context.Background()

	// r, err := rpcClient.Call(ctx, "helloworld3")
	// if err != nil {
	// 	panic(err)
	// }

	// fmt.Printf("%+v\n", r.Result)

	// reply := make(map[string]interface{})

	// reply := struct {
	// 	Return1 int
	// 	Return2 string
	// 	Return3 []string
	// }{}

	// err = rpcClient.CallFor(ctx, &reply, "helloworld3")
	// if err != nil {
	// 	panic(err)
	// }

	// fmt.Printf("%+v\n", reply)

	input := struct {
		FirstString  string
		SecondString string
		Haha         map[string]interface{}
	}{
		FirstString:  "Hello",
		SecondString: "World",
		Haha: map[string]interface{}{
			"first":  "haha",
			"second": "hehe",
		},
	}

	reply := 0
	err := rpcClient.CallFor(ctx, &reply, "helloworld4", &input)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", reply)

	// r, err := rpcClient.Call(ctx, "getDate")
	// if err != nil {
	// 	panic(err)
	// }

	// fmt.Printf("%+v\n", r.Result)

	// generates body: {"method":"getDate","id":0,"jsonrpc":"2.0"}
}
